<?php
/**
 * @copyright   七月
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      July<july491@163.com>
 * 登录验证
 */
namespace app\admin\model;
use think\Model;
use util\Util;

class JuSer extends Model 
{
     protected $pk = 'id';

      /**
     * 添加或编辑用户
     * @param  array 数据
     * @return bool
     */
    public function updateUser($param){
        $data['username']        = $param['username'];
        // $data['group_id']        = $param['group_id'];
        // $data['about']           = $param['about'];
        $data['email']           = $param['email'];
        $data['password']        = $param['password'];
        $data['last_login_ip']   = Util::getIp();
        $data['last_login_time'] = time();
        $data['update_time']     = time();
        if(isset($param['id']) && $param['id'] > 0){
            if(!is_null($param['password'])){
                $data['password']  = password_hash(md5($param['password']),PASSWORD_DEFAULT);
            }
        }else{
            $data['password']  = password_hash(md5($param['password']),PASSWORD_DEFAULT);
        }
        if(isset($param['id']) && $param['id'] > 0){
            return JuSer::where('id',$param['id'])->update($data);
        }else{
            $data['create_time']  = time();
            return JuSer::insert($data);
            if (!$param) {
              
                return json(['code'=>0,'msg'=>'注册失败']);
            
            }
        }  
    }

    /**
     * 判断登录用户
     * @access public
     * @return bool
     */
    public function login($param){
        $condition = [
            'username'=> $param['username'],
            'locks'   => 0,
        ];
        $result = JuSer::get($condition);
        if($result){
            if(!password_verify(md5($param['password']),$result->getAttr('password'))) {
                return FALSE;
            }
            JuSer::updateLogin($result->getAttr('id'));
            return $result;
        }
        return FALSE;
    }

    /**
     * 更新用户登录信息
     * @param  integer $uid 用户ID
     */
    protected function updateLogin($uid){
        $data = array(
            'id'              => $uid,
            'last_login_time' => time(),
            'last_login_ip'   => request()->ip(),
        );
        JuSer::update($data);
    }

    /**
     * 修改我的密码
     * @access public
     */
    public function upDatePasspowrd($param){
        $data = [
            'password' => password_hash(md5($param['password']),PASSWORD_DEFAULT),
            'about'    => $param['about']
        ];
        $where = ['id' => $param['login']['admin_id']];
        return JuSer::where($where)->update($data);
    } 
}

 

